layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;
    var status = $("#status").val();
    //列表
    var tableIns = table.render({
        elem: '#entityList',
        url : '/order/jsonOnlyOrderList',
        cellMinWidth : 95,
        where: {status:status},
        page : true,
        height : "full-125",
        limit : 20,
        method:'post',
        limits : [10,15,20,25],
        id : "entityList",
        cols : [[
            {type: 'numbers', title: '序号'},
            {field: 'id', title: '订单号',align:'center'},
            {field: 'memberName', title: '用户',align:'center'},
            {field: 'payment', title: '实付款',align:'center'},
            {field: 'balanceMoney', title: '余额支付',align:'center'},
            {field: 'addressName', title: '收货人',align:'center'},
            {field: 'addressPhone', title: '收货人手机',align:'center'},
            {field: 'addressDescripts', title: '发货地址',align:'center'},
            {field: 'status', title: '状态',align:'center',templet:function(d){
                if(d.status=="1"){
                    return "待支付";
                }else if(d.status=="2"){
                    return "待发货";
                } else{
                    return "已收货";
                }
            }},
            {field: 'createTime', title: '创建时间', align:'center', minWidth:110, templet:function(d){
                return d.createTime;
            }},
            {title: '操作',width:170,align:"center", templet:function(d){
                if (d.status=="1"){
                    return '<a class="layui-btn layui-btn-xs" lay-event="show">物品详情</a>';
                } else if (d.status=="2"){
                    return '<a class="layui-btn layui-btn-xs" lay-event="show">物品详情</a><a class="layui-btn layui-btn-xs" lay-event="goodsEdit">发货</a>';
                }else{
                    return '<a class="layui-btn layui-btn-xs" lay-event="show">物品详情</a><a class="layui-btn layui-btn-xs" lay-event="del">退货</a>';
                }

            }}
        ]]
    });

    //搜索
    $(".search_btn").on("click",function(){
        table.reload("entityList",{
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                id: $("#id").val(),
                memberName: $("#memberName").val(),
                status:$("#status").val()
            }
        })
    });

    //增加/编辑
    function addEntity(params){
        var title="编辑";
        if(params==null||params==""){
            title="添加"
        }
        var index = layui.layer.open({
            title :title,
            type : 2,
            content : "/order/selectGoodsEntity"+params,
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                body.find(".closeLay").bind("click",function () {
                    layui.layer.close(index);
                })
                layui.layer.full(index);
            }
        });
    }
    $(".add_btn").click(function(){
        addEntity('');
    })

    // 列表操作
    table.on('tool(entityList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑

        } else if(layEvent === 'goodsEdit'){ //编辑
            addEntity("?id="+data.id);
        } else if(layEvent === 'show'){ //编辑
            window.location.href = "/orderItem/listPage"+"?orderId="+data.id;
        } else if(layEvent === 'del'){ //删除
            layer.confirm('是否退货？',{icon:3, title:'提示信息'},function(index){
                $.get("/order/updateReturnStatus",{
                    "id":data.id,
                    "status":"1",
                    "_method":"DELETE"
                },function(result){
                    if(1==result.returnInfo.state){
                        tableIns.reload();
                        layer.close(index);
                    }else{
                        top.layer.msg(result.returnInfo.errorMsg);
                    }
                })
            });
        }
    });
})